<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" >
      <el-form-item label="订单ID" prop="orderId" v-show="showSearch">
        <el-input v-model="queryParams.orderId" placeholder="请输入订单ID" clearable style="width: 240px"  @input="validateInputValue"  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item v-show="showSearch">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <!--      <el-form-item >-->
      <!--        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>-->
      <!--      </el-form-item>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-form>

    <el-table v-loading="loading" :data="rowList" >
      <el-table-column label="质检单号" prop="id"   align="center" width="120"  fixed="left" />
      <el-table-column label="订单ID" prop="orderId" align="center" width="120"/>
      <el-table-column label="订单编号" prop="orderNum" align="center" width="160">
        <template slot-scope="scope">
          <el-link type="primary" @click="openOrder(scope.row)">{{ scope.row.orderNum }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="质检状态" prop="status" align="center" min-width="120">
        <template slot-scope="scope">
          <span :style="{'color': colors(scope.row.status)}">{{ scope.row.status }}</span>
        </template>
      </el-table-column>
      <el-table-column label="质检员" prop="inspector" align="center" width="180"/>
      <el-table-column label="质检时间" align="center" prop="checkTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" prop="remark" align="center" width="200"/>
      <el-table-column label="创建时间" align="center" prop="createTime" min-width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="createTime" min-width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center"  fixed="right"  width="160" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"

          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"

          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改食材配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
         <el-form-item label="备注" prop="remark">
              <el-input  v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { get, update, list } from "@/api/examine/goods/normalExamine";
  export default {
    name: "Normal",
    dicts: ['sys_normal_disable'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 食材表格数据
        rowList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageSize: 10,
          pageNum: 1,
          orderId:'',
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    activated() {
      this.getList()
    },
    created() {
      this.getList();
    },
    methods: {
      validateInputValue() {
        this.queryParams.orderId = this.queryParams.orderId.replace(/[^0-9]/g, ""); // 只允许数字和小数点
      },
      colors(status) {
        if (status === "质检合格") {
          return '#2a9c4a'
        } else if (status === "质检不合格" || status.startsWith("质检不合格")) {
          return '#FF0000'
        } else if (status === "质检待完成") {
          return '#fe9f29'
        } else {
          return '#fe9f29'
        }
      },
      /** 查询列表 */
      getList() {
        this.loading = true;
        list(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
            this.rowList = response.rows;
            this.total = response.total;
            this.loading = false;
          }
        );
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id;
        get(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改点餐质检";
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              update(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              add(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      handleDetail: function(row) {
        const id = row.id;
        this.$router.push("/examine/goods/normal/detail/"+id);
      },
      openOrder(row){
        this.$router.push({
          path:'/order/list/detail',
          query:{
            id:row.orderId
          }
        });
      }
    }
  };
</script>
